package main

import (
	"bytes"
	"github.com/russross/blackfriday"
	"net/http"
	"os"
)

func main() {
	port := os.Getenv()
	if port == "" {
		port = "8080"
	}
	http.HandleFunc("/markdown", GenerateMarkdown)
	http.Handle("/", http.FileServer(http.Dir("./web")))
	http.ListenAndServe(":"+port, nil)
}

func GenerateMarkdown(rw http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		var buf bytes.Buffer
		buf.WriteString("wrong method ")
		buf.WriteString(r.Method)
		rw.Write([]byte(buf.String()))
		return
	}
	markdown := blackfriday.MarkdownCommon([]byte(r.FormValue("body")))
	rw.Write(markdown)
}
